﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using Effects.Effects;

namespace Effects.GUI
{
    /// <summary>
    ///  Wrapper around the <see cref="EffectBoard"/> class that adds buttons to control the effects.
    /// </summary>
    /// <remarks>
    ///  <para>
    ///   This class, on one hand, wraps the <see cref="EffectBoard"/> by adding buttons to control order and removing of the effects. 
    ///   On the other hand it provides also displaying functionality for the <see cref="EffectBoard"/> class.
    ///  </para>
    ///  <para>
    ///   Wrapped <see cref="EffectBoard"/> is accessible through the <see cref="EffectBoardPanel.Board"/> property.
    ///  </para>
    /// </remarks>
    public class EffectBoardPanel:TableLayoutPanel
    {
        /// <summary>
        ///  Creates new instance of EffectBoardPanel with <see cref="EffectBoard"/> ready to use.
        /// </summary>
        public EffectBoardPanel()
        {
            InitializeComponents();
            //this.AutoScroll = true;
        }

        /// <summary>
        ///  Gets or sest the Effect Board
        /// </summary>
        public EffectBoard Board
        {
            get { return board; }
        }

        /// <summary>
        ///  Moves selected effect up.
        /// </summary>
        private Button UpButton;

        /// <summary>
        ///  Moves selected effect down.
        /// </summary>
        private Button DownButton;

        /// <summary>
        ///  Removes selected effect.
        /// </summary>
        private Button RemoveButton;

        /// <summary>
        ///  Launches dialog to edit changes saved previously.
        /// </summary>
        private Button EditChangesButton;

        /// <summary>
        ///  Tells whether <see cref="EffectBoardPanel.EditChangesButton"/> is enabled.
        /// </summary>
        /// <remarks>
        ///  By enabled we mean that it can set enabled/disabled using the <see cref="EffectBoardPanel.EnableButtons"/> method.
        /// </remarks>
        private bool editButtonEnabled;

        /// <summary>
        ///  Wrapped effect board
        /// </summary>
        private EffectBoard board;



        #region initialize

        /// <summary>
        ///  Initializes all componenets.
        /// </summary>
        private void InitializeComponents()
        {
            this.SuspendLayout();

            // Prepares table and its content
            RowCount = 2;
            ColumnCount = 4;

            ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
            ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
            ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
            ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));

            RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
            //RowStyles.Add(new RowStyle(SizeType.Absolute, 200));

            InitializeButtons();
            InitializeBoard();

            this.ResumeLayout(false);
            this.PerformLayout();
        }

        
        /// <summary>
        ///  Intializes buttons and adds them to table layout
        /// </summary>
        private void InitializeButtons()
        {
            // Up button
            UpButton = new Button();
            UpButton.Text = "Left";
            UpButton.Name = "Up";
            UpButton.Dock = DockStyle.Fill;
            UpButton.Click += new EventHandler(Up_Clicked);

            // Down button
            DownButton = new Button();
            DownButton.Text = "Right";
            DownButton.Name = "Down";
            DownButton.Dock = DockStyle.Fill;
            DownButton.Click += new EventHandler(Down_Clicked);

            // Remove button
            RemoveButton = new Button();
            RemoveButton.Text = "Remove";
            RemoveButton.Name = "Remove";
            RemoveButton.Dock = DockStyle.Fill;
            RemoveButton.Click += new EventHandler(Remove_Clicked);

            // Edit changes button
            EditChangesButton = new Button();
            EditChangesButton.Text = "Edit settings changes";
            EditChangesButton.Name = "SaveInitials";
            EditChangesButton.Dock = DockStyle.Fill;
            EditChangesButton.Click += new EventHandler(EditChanges_Clicked);

            // Add buttons
            Controls.Add(RemoveButton, 0, 0);
            Controls.Add(UpButton, 1, 0);
            Controls.Add(DownButton, 2, 0);
            Controls.Add(EditChangesButton, 3, 0);
        }

        /// <summary>
        ///  Initializes EffectBoard
        /// </summary>
        private void InitializeBoard()
        {
            /*
            Panel panel = new Panel();
            panel.AutoScroll = true;
            panel.Dock = DockStyle.Fill;
            Controls.Add(panel, 0, 1);
            SetColumnSpan(panel, 4); */

            board = new EffectBoard();
            board.Location = new System.Drawing.Point(0, 0);
            board.Dock = DockStyle.Fill;
            Controls.Add(board, 0, 1);
            SetColumnSpan(board,4);

            //panel.Controls.Add(board);
        }
        #endregion


        /// <summary>
        ///  Enable or disables buttons.
        /// </summary>
        /// <param name="enable"> true - enabled, false = disabled </param>
        /// <remarks>
        ///  <para> 
        ///   This method enables (disables) all buttons unless <see cref="EffectBoardPanel.EditChangesButton"/> is disabled. If so, this button is ignored in the method.
        ///  </para>
        /// </remarks>
        public void EnableButtons(bool enable)
        {
            UpButton.Enabled = enable;
            DownButton.Enabled = enable;
            RemoveButton.Enabled = enable;
            if(editButtonEnabled)
                EditChangesButton.Enabled = enable;
        }


        /// <summary>
        ///  Sets <see cref="EffectBoardPanel.EnableButtons"/> method to include/ignore <see cref="EffectBoardPanel.EditChangesButton"/>.
        /// </summary>
        /// <param name="enabled"> true - include, false - ignore</param>
        public void EnableEditChangesButton(bool enabled)
        {
            this.editButtonEnabled = enabled;
            this.EditChangesButton.Enabled = enabled;
        }

        /// <summary>
        /// Adds <see cref="global::Effects.Effects.Effect"/> to the board
        /// </summary>
        /// <param name="e"> <see cref="global::Effects.Effects.Effect"/> to add </param>
        public void AddEffect(Effect e)
        {
            board.Add(e);
        }

        // Event handlers for buttons
        #region button_handlers

        /// <summary>
        ///  <see cref="EffectBoardPanel.RemoveButton"/> event handler
        /// </summary>
        /// <param name="sender"> Event sender. </param>
        /// <param name="e"> Event arguments. </param>
        private void Remove_Clicked(object sender, EventArgs e)
        {
            board.RemoveSelected();
        }

        /// <summary>
        ///  <see cref="EffectBoardPanel.UpButton"/> event handler
        /// </summary>
        /// <param name="sender"> Event sender. </param>
        /// <param name="e"> Event arguments. </param>
        private void Up_Clicked(object sender, EventArgs e)
        {
            board.MoveUp();
        }

        /// <summary>
        ///  <see cref="EffectBoardPanel.DownButton"/> event handler
        /// </summary>
        /// <param name="sender"> Event sender. </param>
        /// <param name="e"> Event arguments. </param>
        private void Down_Clicked(object sender, EventArgs e)
        {
            board.MoveDown();
        }

        /// <summary>
        ///  <see cref="EffectBoardPanel.EditChangesButton"/> event handler
        /// </summary>
        /// <param name="sender"> Event sender. </param>
        /// <param name="e"> Event arguments. </param>
        private void EditChanges_Clicked(object sender, EventArgs e)
        {
            board.EditChanges();
        }

        #endregion
    }
}
